'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 购买商品
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var dataApi = require('../../../../libs/dataApi');
var rewardType = require('../../../../consts/rewardType');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var vipConf = require('../../../../config/vipConf');
var vipShopConf = require('../../../../config/vipShopConf');
var rewardType = require('../../../../consts/rewardType');
var rewardService = require('../../../../services/rewardService');
var roleService = require('../../../../services/roleService');
var taskService = require('../../../../services/taskService');
var vipConf = require('../../../../config/vipConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var shop_type = msg.shop_type;
    var has_buy_num;
    if (!shop_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall([
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'build', 'shop', 'tech', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var resource_shop_conf = dataApi.resource_shop.findById(shop_type);
                if (!resource_shop_conf) {
                    return;
                }

                // 是否购买次数达到上限
                if (!_isHaveBuyNum(player_manager, shop_type, resource_shop_conf.time_limit)) {
                    return next(null, {
                        code: Code.USER_BUY_RESIYRCE_SHOP_NOT_NUM
                    });
                }
                has_buy_num = player_manager.shop.getBuyResourceShopNum(shop_type);

                // 存储购买次数
                player_manager.shop.addBuyResourceShopNum(shop_type);
                var vip_lv = player_manager.role.getVip();
                var discount = (vipConf.getConf(vip_lv).shop_discount / 10) || 1;
                var sub_diamond = parseInt(resource_shop_conf.price * discount);

                // 扣除钻石
                if (!roleService.subDiamond(player_manager, moduleType.MDL_RES_SHOP, operationType.OPR_RES_SHOP_BUY, sub_diamond, shop_type)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 发放资源奖励
                rewardService.addRewardToRole(player_manager, resource_shop_conf, moduleType.MDL_RES_SHOP, operationType.OPR_RES_SHOP_BUY);

                // 增加任务统计
                taskService.addBuyResources(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    has_buy_num: has_buy_num
                }
            });
        }
    );
};

/**
 * 是否还有购买次数
 */
var _isHaveBuyNum = function (player_manager, shop_type, daily_buy_shop_num) {
    var has_buy_num = player_manager.shop.getBuyResourceShopNum(shop_type);
    var total_buy_num = vipShopConf.getVipBuyShopNum(shop_type, player_manager.role.getVip()) + daily_buy_shop_num;
    if (has_buy_num < total_buy_num) {
        return true;
    } else {
        return false;
    }
};
